---
title: 'prev | Cypress Documentation'
description: Get the immediately preceding sibling of each element in a set of the elements in Cypress.
sidebar_label: prev
slug: /api/commands/prev
---

<ProductHeading product="app" />

# prev

Get the immediately preceding sibling of each element in a set of the elements.

:::info

The querying behavior of this command matches exactly how
[`.prev()`](http://api.jquery.com/prev) works in jQuery.

:::

## Syntax

```javascript
.prev()
.prev(selector)
.prev(options)
.prev(selector, options)
```

### Usage

<Icon name="check-circle" color="green" /> **Correct Usage**

```javascript
cy.get('tr.highlight').prev() // Yield previous 'tr'
```

<Icon name="exclamation-triangle" color="red" /> **Incorrect Usage**

```javascript
cy.prev() // Errors, cannot be chained off 'cy'
cy.getCookies().prev() // Errors, 'getCookies' does not yield DOM element
```

### Arguments

<Icon name="angle-right" /> **selector _(String selector)_**

A selector used to filter matching DOM elements.

<Icon name="angle-right" /> **options _(Object)_**

Pass in an options object to change the default behavior of `.prev()`.

| Option    | Default                                                           | Description                                                                         |
| --------- | ----------------------------------------------------------------- | ----------------------------------------------------------------------------------- |
| `log`     | `true`                                                            | Displays the command in the [Command log](/app/core-concepts/open-mode#Command-Log) |
| `timeout` | [`defaultCommandTimeout`](/app/references/configuration#Timeouts) | Time to wait for `.prev()` to resolve before [timing out](#Timeouts)                |

<HeaderYields />

- `.prev()` yields the new DOM element(s) it found.
- `.prev()` is a query, and it is _safe_ to chain further commands.

## Examples

### No Args

#### Find the previous element of the element with class of `active`

```html
<ul>
  <li>Cockatiels</li>
  <li>Lorikeets</li>
  <li class="active">Cockatoos</li>
  <li>Conures</li>
  <li>Eclectus</li>
</ul>
```

```javascript
// yields <li>Lorikeets</li>
cy.get('.active').prev()
```

### Selector

#### Find the previous element with a class of `active`

```html
<ul>
  <li>Cockatiels</li>
  <li>Lorikeets</li>
  <li class="active">Cockatoos</li>
  <li>Conures</li>
  <li>Eclectus</li>
</ul>
```

```javascript
// yields <li>Cockatoos</li>
cy.get('li').prev('.active')
```

## Rules

<HeaderRequirements />

- `.prev()` requires being chained off a command that yields DOM element(s).

<HeaderAssertions />

- `.prev()` will automatically [retry](/app/core-concepts/retry-ability)
  until the element(s)
  [exist in the DOM](/app/core-concepts/introduction-to-cypress#Implicit-Assertions).
- `.prev()` will automatically [retry](/app/core-concepts/retry-ability)
  until all chained assertions have passed.

<HeaderTimeouts />

- `.prev()` can time out waiting for the element(s) to
  [exist in the DOM](/app/core-concepts/introduction-to-cypress#Implicit-Assertions).
- `.prev()` can time out waiting for assertions you've added to pass.

## Command Log

**_Find the previous element of the active `li`_**

```javascript
cy.get('.left-nav').find('li.active').prev()
```

The commands above will display in the Command Log as:

<DocsImage
  src="/img/api/prev/find-prev-element-in-list-of-els.png"
  alt="Command Log prev"
/>

When clicking on `prev` within the command log, the console outputs the
following:

<DocsImage
  src="/img/api/prev/previous-element-in-console-log.png"
  alt="Console Log prev"
/>

## See also

- [`.next()`](/api/commands/next)
- [`.prevAll()`](/api/commands/prevall)
- [`.prevUntil()`](/api/commands/prevuntil)
